﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 
using U9API.Pub;
using System.Data;
using U9API.JsonModel;
using Newtonsoft.Json;
using System.Reflection;

namespace U9API.APIHelper
{
    public class RecBillHead
    {
        public Int64 ID { get; set; }
        public string DocNo { get; set; }
        public Int64 DocumentType { get; set; }
        public int DocStatus { get; set; }
        public string Cust_Code { get; set; }
        public string  Cust_ShortName { get; set; }
        public string CustSite_Code { get; set; }
        public Int64 CCObjSite { get; set; } 
        public int RecObjType { get; set; }
        public string RecDate { get; set; }
        public Int64 PostPeriod { get; set; }

        public Int64 Cust_Customer { get; set; }
        public Int64 CustSite_CustomerSite { get; set; }

        public Int64 Transactor { get; set; }
        public string TransactorName { get; set; }
        public Int64 Dept { get; set; }
        public string DeptName { get; set; }

        public string Note { get; set; }

        public string DeptCode { get; set; }
        public string TransactorCode { get; set; }

        public List<RecBillLine> RecBillLines { get; set; }
    }

    public class RecBillLine
    {
        public Int64 ID { get; set; }
        public int LineNum { get; set; }
        public Int64 SettlementMethod { get; set; }
        public string NoteNo { get; set; }
        public decimal Money { get; set; }
        public decimal RecACCSettlementFee { get; set; }
        public decimal TotalMoney { get; set; }
        public Int64 RecBkAccount { get; set; }
        public string RecBillUseLines { get; set; }
    }
     
    

    public class RecBillQueryHelper : BaseHelper<JsonModel.ApiQuery>
    { 
        public List<U9API.DBHelper.Model.Customer> Resdatas { get; set; }
        public string result;


        private RecBillHead DataRowToHModel(DataRow row)
        {
            RecBillHead model = new RecBillHead();
            Type examType = typeof(RecBillHead);
            BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
            if (row != null)
            {
                DataColumnCollection dc = row.Table.Columns;
                for (int i = 0; i < dc.Count; i++)
                {
                    if (row[dc[i].ColumnName] != null && row[dc[i].ColumnName].ToString() != "")
                    {
                        PropertyInfo piInstance = examType.GetProperty(dc[i].ColumnName, flag);
                        if (piInstance != null)
                        {
                            piInstance.SetValue(model, row[dc[i].ColumnName], null);
                        }
                    }
                }
            }

            return model;
        }

        private RecBillLine DataRowToBModel(DataRow row)
        {
            RecBillLine model = new RecBillLine();

            Type examType = typeof(RecBillLine);
            BindingFlags flag = BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.Instance;
            if (row != null)
            {
                DataColumnCollection dc = row.Table.Columns;
                for (int i = 0; i < dc.Count; i++)
                {
                    if (row[dc[i].ColumnName] != null && row[dc[i].ColumnName].ToString() != "")
                    {
                        PropertyInfo piInstance = examType.GetProperty(dc[i].ColumnName, flag);
                        if (piInstance != null)
                        {
                            piInstance.SetValue(model, row[dc[i].ColumnName], null);
                        }
                    }
                }
            }
            return model;
        }

        protected override void Create(string U9constr, int operation, List<JsonModel.ApiQuery> models )
        {
            //Pub.WriteLog.WriteLine(DateTime.Now.ToShortDateString() + ": RecBillQueryHelper");
            if (models.Count == 0)
            {
                throw new Exception("查询条件不允许为空!");
            }
            JsonModel.ApiQuery model = models[0];
            StringBuilder sql = new StringBuilder();
            sql.Append("select  A7.Code as TransactorCode, A8.Code as DeptCode, a.ID, a.DocNo, a.DocumentType , A3.Note, Transactor, A1.NAME AS TransactorName, A.Dept, A2.Name as DeptName, DocStatus, Cust_Customer, CustSite_CustomerSite,  Cust_Code, Cust_ShortName, CustSite_Code, CCObjSite ,RecObjType, Convert(varchar(10), RecDate,121) as RecDate, PostPeriod ");
            sql.Append(" from AR_RecBillHead a  ");
            sql.Append(" LEFT JOIN CBO_Operators_Trl A1 ON A1.ID = a.Transactor ");
            sql.Append(" LEFT JOIN CBO_Department_Trl A2 ON A2.ID = a.Dept ");
            sql.Append(" LEFT JOIN AR_RecBillHead_Trl A3 on A3.ID = a.ID ");
            sql.Append(" LEFT JOIN CBO_Operators A7 ON A7.ID = A.Transactor ");
            sql.Append(" LEFT JOIN CBO_Department A8 on A8.ID = A.Dept");
            sql.Append(" where (DocStatus = 2 or DocStatus = 4) "); 
           
            if (!string.IsNullOrEmpty(model.code))
            {
                sql.Append("and a.docno = '" + model.code + "'");
            }
            if (!string.IsNullOrEmpty(model.id))
            {
                sql.Append("and a.ID = '" + model.id + "'");
            }
            if (!string.IsNullOrEmpty(model.customer))
            {
                sql.Append("and a.Cust_Customer = '" + model.customer + "'");
            }
            if (!string.IsNullOrEmpty(model.startdate))
            {
                sql.Append(" and (a.CreatedOn >='" + model.startdate + "' or a.ModifiedOn >='" + model.startdate +"')");
            }
            if (!string.IsNullOrEmpty(model.enddate))
            {
                sql.Append(" and (a.CreatedOn <='" + model.enddate + "' or a.ModifiedOn <='" + model.enddate + "')");
            }
            //Pub.WriteLog.WriteLine(sql.ToString());
            DataSet ds = DbHelperSQL.Query(sql.ToString(), U9constr);
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                List<RecBillHead> mlist = new List<RecBillHead>();
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    mlist.Add(DataRowToHModel(ds.Tables[0].Rows[i]));
                }

                foreach (var item in mlist)
                {
                    StringBuilder sqlb = new StringBuilder();
                    sqlb.Append("select a.ID, a.LineNum, a.SettlementMethod, b.NoteNo, a.Money_OCMoney as Money, a.RecACCSettlementFee_OCMoney as RecACCSettlementFee, a.TotalMoney_OCMoney as TotalMoney, a.RecBkAccount, u.RecProperty ");
                    sqlb.Append(" from AR_RecBillLine a left join  [AR_RecBillLine_Trl] b  on (b.SysMlFlag = 'zh-CN') and (a.[ID] = b.[ID]) ");
                    sqlb.Append(" left join AR_RecBillUseLine u on u.RecBillLine = b.ID ");
                    sqlb.Append(" where u.SrcUseLineID = -1 and a.RecBillHead = '" + item.ID +"' ");
                    //Pub.WriteLog.WriteLine(sql.ToString());

                    DataTable dtb = DbHelperSQL.QueryAsDT(sqlb.ToString(), U9constr);
                    if (dtb!= null && dtb.Rows.Count > 0)
                    {
                        item.RecBillLines = new List<RecBillLine>();
                        for (int i = 0; i < dtb.Rows.Count; i++)
                        {
                            item.RecBillLines.Add(DataRowToBModel(dtb.Rows[i]));
                        }
                    }
                }
                result = mlist.ToJson();

            }
            else
            {
                throw new Exception("没有查到匹配的收款单记录!");
                
            }
        }
         
    }
}
